// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.ProgressTo = cc.Class({
    name: 'cc.ProgressTo',
    extends: cc.ActionInterval,

    ctor:function (duration, progress) {
        this._startProgress = 0;
        this._endProgress = 1;
        this._deltaProgress = 0;
        progress !== undefined && cc.ProgressTo.prototype.initWithDuration.call(this, duration, progress);
    },

    /*
     * Initializes the action.
     * @param {Number} duration
     * @param {Number} progress
     * @return {Boolean}
     */
    initWithDuration:function (duration, progress) { //function overload here
        if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
            this._endProgress = progress;
            return true;
        }
        return false;
    },

    clone:function () {
        var action = new cc.ProgressTo();
        this._cloneDecoration(action);
        action.initWithDuration(this._duration, this._endProgress);
        return action;
    },

    startWithTarget:function (target) {
        cc.ActionInterval.prototype.startWithTarget.call(this, target);

        var com = target.getComponent(cc.ProgressBar);
        if (com) {
            this._startProgress = com.progress;
        }
        this._deltaProgress = this._endProgress - this._startProgress;
    },

    update:function (dt) {
        dt = this._computeEaseTime(dt);
        if (this.target) {
            var com = this.target.getComponent(cc.ProgressBar);
            if (com) {
                com.progress = this._startProgress + this._deltaProgress * dt;
            } 
        }
    }
});

cc.progressTo = function (duration, progress) { //function overload
    return new cc.ProgressTo(duration, progress);
};
